<?php
App::uses('AppModel', 'Model');
/**
 * Subject Model
 *
 * @property Term $Term
 * @property User $User
 * @property Branchsubject $Branchsubject
 * @property Exam $Exam
 * @property Studentsubject $Studentsubject
 * @property Teachersubject $Teachersubject
 */
class Subject extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		 'oral_limit' => array(
			  'numeric' => array(
			  'rule' => array('numeric'),
			  'message' => 'Your custom message here',
	          'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
			 'oral_success' => array(
			 'numeric' => array(
			 'rule' =>array('CheckMarkLimit','oral_limit','oral_success'),
	         'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
			  'written_limit' => array(
			  'numeric' => array(
			  'rule' => array('numeric'),
			  'message' => 'Your custom message here',
	          'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
			 'written_success' => array(
			 'numeric' => array(
			 'rule' =>array('CheckMarkLimit','written_limit','written_success'),
	         'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		  'midterm_limit' => array(
			  'numeric' => array(
			  'rule' => array('numeric'),
			  'message' => 'Your custom message here',
	          'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
			 'midterm_success' => array(
			 'numeric' => array(
			 'rule' =>array('CheckMarkLimit','midterm_limit','midterm_success'),
	         'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		 'practical_limit' => array(
			  'numeric' => array(
			  'rule' => array('numeric'),
			  'message' => 'Your custom message here',
	          'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
			 'practical_success' => array(
			 'numeric' => array(
			 'rule' =>array('CheckMarkLimit','practical_limit','practical_success'),
	         'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		 'mark_limit' => array(
			  'numeric' => array(
			  'rule' => array('CheckMarkall'),
			  'message' => 'Your custom message here',
	          'allowEmpty' => false,
			  //'required' => true,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
			 'mark_success' => array(
			 'numeric' => array(
			 'rule' =>array('CheckMarkLimit','mark_limit','mark_success'),
	         'message' => 'Your custom message here',
			 'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'term_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
public function CheckMarkLimit($data,$fieldlimit,$fieldsuccess) {
      // $data will contain array('success' => 'value')
      if($this->data[$this->alias][$fieldlimit]=="" && $this->data[$this->alias][$fieldsuccess]==""){
      return true;}
      elseif ($this->data[$this->alias][$fieldsuccess]!="" ) {
          return ( $this->data[$this->alias][$fieldsuccess] <= $this->data[$this->alias][$fieldlimit])?true:false;
      }else{return false;}
	 }

	 
public function CheckMarkall($data) {
      
      $subject_parts=array($this->data[$this->alias]['oral_limit'],
                          $this->data[$this->alias]['written_limit'],
                          $this->data[$this->alias]['midterm_limit'],
                        $this->data[$this->alias]['practical_limit']);
      if(current($data)==array_sum($subject_parts)){
      return true;}else{return false;}
	 }
	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Term' => array(
			'className' => 'Term',
			'foreignKey' => 'term_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Branchsubject' => array(
			'className' => 'Branchsubject',
			'foreignKey' => 'subject_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Exam' => array(
			'className' => 'Exam',
			'foreignKey' => 'subject_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Studentsubject' => array(
			'className' => 'Studentsubject',
			'foreignKey' => 'subject_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Teachersubject' => array(
			'className' => 'Teachersubject',
			'foreignKey' => 'subject_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
